<div class="modal j_modalDomain">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close"><span>×</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">设置涉及领域</h4>
            </div>
            <div class="modal-body" style="max-height: 400px;">
                <div class="crm-tree" id="crmTreeMenu">
                    <input class="form-control" id="crmTreeFilter" type="text" placeholder="输入关键字进行检索" style="float: none">
                    <ul>
                        @forelse($firstDomainList as $key=>$value)
                            <li class="parent"><label><input type="checkbox" value="{{$value['id']}}"><span>{{$value['name']}}</span></label>
                                <ul>
                                    @forelse($domainList[$value['id']] as $k=>$v)
                                        <li><label><input type="checkbox" value="{{$v['id']}}"><span>{{$v['name']}}</span></label></li>

                                    @empty
                                    @endforelse
                                </ul>
                            </li>
                        @empty
                        @endforelse
                    </ul>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary">确定</button>
            </div>
        </div>
    </div>
</div>
<script>
    (function ($) {
        $(function () {
            var $m = $('.j_modalDomain');
            var _crmTreeMenu = $('#crmTreeMenu');
            initTree();
            function initTree() {
                //初始化是否有子菜单
                _crmTreeMenu.find('li').each(function () {
                    var _t = $(this);
                    if (_t.children('ul').length > 0) {
                        _t.addClass('parent');
                    }
                });
                //初始化选中状态
                var ids = $('#j_domainIds').val();
                if (ids) {
                    ids = ',' + ids.toString() + ',';
                    _crmTreeMenu.find('input').each(function () {
                        var _t = $(this);
                        if ((ids).indexOf(',' + _t.val() + ',') != -1) {
                            _t.prop('checked', true);
                        }
                    });
                }
            }

            //确认
            $m.find(".btn").click(function () {
                var $btn = $(this);
                var _chk = _crmTreeMenu.find('input[type="checkbox"]:checked');
                if (_chk.length === 0) {
                    //模拟失败操作
                    //$.Alert({obj: $btn, txt: '至少选择一个技术路径！', type: 'danger'});
                    //return false;
                    $(".modal").parent().fadeOut(200);
                } else {
                    //成功操作
                    var i = 0, domainHtml = '', domainIds = ',';
                    for (; i < _chk.length; i++) {
                        domainIds += _chk.eq(i).val() + ',';
                        domainHtml += '<span>' + _chk.eq(i).next().html() + '<input class="j_domainId" type="hidden" value="' + _chk.eq(i).val() + '"></span>';
                    }
                    $.Alert({
                        obj: $btn, txt: '设置成功！', callback: function () {
                            $("#j_alert").remove();
                            $m.parent().fadeOut(200);
                            $('#j_domainIds').val(domainIds);
                            $(".j_domainItems").css('display', 'inline-block').html(domainHtml);
                            $.ajax({
                                url: '/get_domain_tech_route_info',
                                headers: {
                                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                                },
                                data: {
                                    domain: domainIds
                                },
                                type: 'post',
                                dateType: 'json',
                                success: function (data) {
                                    var result = JSON.parse(data);
                                    if (result.result==-1) {
                                        $(".domain_tech_flag").hide();
                                    }else{
                                        $(".domain_tech_flag").show();
                                    }
                                }
                            })
                        }
                    });
                }
            });

            //点击切换子菜单显示
            _crmTreeMenu.find('li').click(function (e) {
                e.stopPropagation();
                var _t = $(this);
                _t.toggleClass('toggle').children('ul').toggle();
            });

            //点击文字显示右侧内容
            _crmTreeMenu.find('label').click(function (e) {
                e.stopPropagation();
            });
            _crmTreeMenu.find('input').click(function (e) {
                e.stopPropagation();
                var _t = $(this);
                var _p = _t.parent('label');
                var id = _t.val();
                if (_t.prop('checked')) {
                    _p.parents('ul').prev().find('input[type="checkbox"]').prop('checked', true);
                } else {
                    _p.next().find('input[type="checkbox"]').removeProp('checked');
                }
            });

            //输入框快速定位
            var startSearch = true;
            $('#crmTreeFilter').bind('change keyup blur', function () {
                if (startSearch) {
                    startSearch = false;
                    setTimeout(treeSearh, 500);
                }
            });
            function treeSearh() {
                var key = $('#crmTreeFilter').val();
                _crmTreeMenu.find('li').css('display', 'none');
                _crmTreeMenu.find('label').each(function () {
                    var _t = $(this);
                    if (_t.html().indexOf(key.toLowerCase()) >= 0) {
                        _t.parents('li').css('display', 'block');
                    }
                });
                startSearch = true;
            }
        });
    })(jQuery);
</script>
